<?php
$getDevice = "SELECT * FROM tb_deviceinfo WHERE di_userId =".$_SESSION[userID]." ORDER BY di_deviceId DESC";
$rowDevice = $db->query($getDevice);
$addCount = $db->affected_rows;
?>
<script language="javascript">
var ajax1=new sack();
function funEditUser(uid,cid,act)
{
	document.frmSubmit.txtDeviceId.value = uid;
	document.frmSubmit.txtClientId.value = cid;
	document.frmSubmit.action = act;
	document.frmSubmit.submit();
}
function toggleImmobalize(btnId,devId,clientId)
{
	
	if(document.getElementById(btnId).value == "Stop Vehicle")
	{
		flag = 1;
	}
	else if(document.getElementById(btnId).value == "Start Vehicle")
	{
		flag = 2;
	}
	document.getElementById(btnId).value = 'Sending...';
	document.getElementById(btnId).disabled = true;
	ajax1.requestFile = 'ajax_server.php?immbolize='+flag+'&devId='+devId+'&cltId='+clientId+'&immId='+document.getElementById('statsImmob').value;
	//alert(ajax1.requestFile);
	ajax1.onCompletion = function(){immoblizeStatus(btnId)};
	ajax1.runAJAX();
}

function immoblizeStatus(id)
{
	if(ajax1.response == -1 )
	{
		document.getElementById(id).value = 'Stop Vehicle';
		document.getElementById(id).disabled = false;
		document.getElementById(id).className = 'showlive_on';
		document.getElementById('statsImmob').value = 0;
	}
	else  if(ajax1.response != -1 && ajax1.response != 0 )
	{
		document.getElementById(id).value = 'Start Vehicle';
		document.getElementById(id).disabled = false;
		document.getElementById(id).className = 'showlive_off';
		document.getElementById('statsImmob').value = ajax1.response;
	}
	else if(ajax1.response == 0 )
	{
		alert("Message delivery failed")
	}
	
}
</script>
<div align="left" class="listofusers">&nbsp;<span><?php require_once("../../includes/recordsPerPage.php"); ?></span></div>
<table class="gridform_final" >
<?php
    //$sql = "SELECT * FROM tb_deviceinfo WHERE di_userId =".$_SESSION[userID]." AND di_clientId = ".$_SESSION[clientID]." ORDER BY di_deviceId DESC";
	$sql = "SELECT * FROM tb_deviceinfo,tb_client_subscription,tb_clientinfo WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_status = 1 AND di_clientId=ci_id AND ci_id=".$_SESSION[clientID]." order by di_deviceName,di_deviceId,ci_clientName ASC";
	$rows = $db->query($sql);
	$tot=$db->affected_rows;
	$sql .=" limit ".$s.",".$n;
	$rows = $db->query($sql);
	
	$i = 0;
   if($tot > 0)
   {
   	while ($record = $db->fetch_array($rows)) 
	{
		//print_r($record);
		if($record[di_deviceName])
			$devName = $record[di_deviceName];
		else
			$devName = $record[di_deviceId];
		
		$renewDate = date("d-m-Y",strtotime("-1 days ".($record[tcs_noOfMonths]) ."months ".$record[tcs_renewalDateFrom]));
		
		if(strtotime(date("d-m-Y")) <= strtotime($renewDate))
		{
			$expStatus = "<span>Monthly subscription will expire on ".$renewDate."</span>";
		}
		else
		{
			$expStatus = "<span style='color:red'>Monthly subscription expired </span>";
		}
		
		$getInsurance = "select * from tb_device_insurance_info where tdii_deviceId = ".$record[di_id];
		$resInsurance = $db->query($getInsurance);
		if($db->affected_rows >0)
		{
			$fetInsurance = $db->fetch_array($resInsurance);
			$insExpDate ="<span>Insurance will expire on ". $fetInsurance[tdii_policyExpDate]."</span>";
		}
		else
		{
			$insExpDate ="Nil";
		}
		
		if($record[di_deviceModel] > 0)
		{
			$sql1 = "SELECT * FROM tb_device_model WHERE id = ".$record[di_deviceModel];
			$rows1 = $db->query($sql1);
			if($db->affected_rows > 0)
			{
				$deviceModelRecord = $db->fetch_array($rows1);
				$devModel = $deviceModelRecord[tdm_aliasName];
			}			
		}else $devModel = "Not Selected";
		
	?>
    		<tr>
            <td width="13%" style="border:none; vertical-align:top;">
            	<table width="100%" border="0">
                  <tr>
                    <td align="center"><img src="unit_img/<?php echo $record[di_deviceImg];?>" width="55" height="55" title="<?php echo ucfirst($devName);?>" alt="<?php echo ucfirst($devName);?>" /></td>
                  </tr>
                  <tr>
                    <th><?php echo ucfirst($devName);?></th>
                  </tr>
            	</table>
            </td>
            <td valign="top" style="border:0; vertical-align:top; ">
            <table width="100%" class="grid_table">
                  <tr>
                    <th width="25%"><span class="form_text">Device Id :</span> &nbsp; <?php echo $record[di_deviceId];?></th>
                    <th width="25%"><span class="form_text">IMEI :</span> &nbsp; <?php echo $record[di_imeiId];?></th>
                    <th width="25%" ><span class="form_text">Model :</span> &nbsp; <?php echo ucfirst($devModel);?></th>
                    <th width="25%" ><span class="form_text">Mobile :</span> &nbsp; <?php echo $record[di_mobileNo];?></th>
                  </tr>
              		<tr>
                        <td width="210" align="left" colspan="3"><span class="form_text">Subs .Status : </span> &nbsp; <?php echo $expStatus;?></td>
                        <td align="left" width="116">
						<!--<img src="../../images/edit.gif" style="cursor:pointer;" width="45" height="16" border="0" onclick="funEditUser('<?php echo $record[di_id];?>','<?php echo $_SESSION[clientID];?>','?ch=addClientDevice')" />-->&nbsp;
						</td>
                  </tr>
                  <?php if($record[di_immoblize] == 1) { ?>
                    <tr>
                        <td align="right">
                        <span class="form_text">Immobalize status : </span></td>
                        <td align="left" colspan="3" width="116">
                        <?php 
							$getCont =  "select * from tb_immbolize_info where tii_devId = ".$record[di_id]." AND tii_clientId =".$_SESSION[clientID]." order by tii_id DESC limit 0 ,1";
							$resCont = $db->query($getCont);
							if($db->affected_rows == 0)
							{
						?>
                        	<input type="button" name="btnImmobalize" class="showlive_on" id="btnImmobalize" value="Stop Vehicle" onclick="toggleImmobalize('btnImmobalize','<?php echo $record[di_id];?>','<?php echo $_SESSION[clientID];?>')" />
                            <input type="hidden" name="statsImmob" id="statsImmob" value="0" />
                        <?php
							} 
							else
							{
								$fetCont = $db->fetch_array($resCont);
								//print_r($fetCont);
								if($fetCont[tii_status ] == 1)
								{
									?>
                                	<input type="button" name="btnImmobalize" class="showlive_off" id="btnImmobalize" value="Start Vehicle" onclick="toggleImmobalize('btnImmobalize','<?php echo $record[di_id];?>','<?php echo $_SESSION[clientID];?>')" />
                                    <input type="hidden" name="statsImmob" id="statsImmob" value="<?php echo $fetCont[tii_id];?>" />
                                <?php
								}
								elseif($fetCont[tii_status ] == 2)
								{
								?>
                                	<input type="button" name="btnImmobalize" class="showlive_on" id="btnImmobalize" value="Stop Vehicle" onclick="toggleImmobalize('btnImmobalize','<?php echo $record[di_id];?>','<?php echo $_SESSION[clientID];?>')" />
                                    <input type="hidden" name="statsImmob" id="statsImmob" value="0" />
                                <?php
								}
							}
						?>
                        <br />
                        <span class="info_port">Use this button under risk factors/conditions only.</span>
						</td>
                  </tr>
                  <?php } ?>
             </table>
            </td>
          </tr>  
    	

    <?php
	$i++;
	}
	if($tot >= 6)
	{
 ?>
 <tr>
	<td colspan="2" style="border-right:0px;"><?php paginate($tot,$n,$pg); ?></td>
  </tr>
 <?php 
 	} 
	}
	else
	{
   ?>
        <td colspan="2" style="border-right:0px;"><span>Not data found</span></td>
   <?php
	}
   ?>
</table>
<form name="formpagination" id="formpagination" action="#" method="post">
  <input type="hidden" name="pn" id="pn" value="<?php if(isset($_POST[pn])) echo $_POST[pn]; else echo '1'; ?>" />
  <input type="hidden" name="nr" id="nr" value="<?php echo $_POST[nr]?>" />
  <input type="hidden" name="pageSelect" value="" />
</form>
<form name="frmSubmit" id="frmSubmit" method="post">
	<input type="hidden" name="txtDeviceId" id="txtDeviceId" />
    <input type="hidden" name="txtClientId" id="txtClientId" />
</form>